/*
*description:商品详情页面的store
*author:张湘
*date:2018-08-27
*/
const state = {
  count: 0,    //购物车内商品数量
  cartList: [],    //购物车内商品详情
  selectList: [],    //购物车内选中要支付的商品
  accountList: [],
  numList: [],      //购物车内单一商品的数量
//   totalPrice: 0       //购物车内所选商品总价
}

const mutations = {
  addCarList(state,payload) {
      state.count++;
      state.cartList.push(payload);
    //   sessionStorage.setItem('carList',JSON.stringify( state.cartList));
  },
  substract(state, payload) {
    // state.cartList=JSON.parse( sessionStorage.getItem( 'carList'))
    if(state.cartList[payload].numbers>1) {
        state.cartList[payload].numbers--;
    }
    
  },
  add(state, payload) {
    // state.cartList=JSON.parse( sessionStorage.getItem( 'carList'))
      state.cartList[payload].numbers++;
  },
  selected(state) {
    // state.cartList=JSON.parse( sessionStorage.getItem( 'carList'))
     for(let i=0; i < state.cartList.length; i++) {
         if(state.cartList[i].isChecked) {
            state.selectList.push(state.cartList[i])
         }
     }
     state.accountList = state.selectList
     state.selectList = []
     console.log(state.selectList)
     //从carList中删除去结算的商品
     for(let i=0;i<state.cartList.length;i++) {
        if(state.cartList[i].isChecked) {
            state.cartList.splice(i,1)
            state.count--
        }
    }
    console.log(state.cartList)
  },
  deleteCart(state, payload) {
      state.cartList.splice(payload,1)
  }
}

const actions = {
  addCarList(context, payload) {
      context.commit('addCarList', payload)
  },
  substract(context, payload) {
      context.commit('substract', payload)
  },
  add(context, payload) {
      context.commit('add', payload)
  },
  selected(context) {
      context.commit('selected')
  },
  deleteCart(context, payload) {
      context.commit('deleteCart',payload)
  }
}

export default {
  state,
  mutations,
  actions
}